package MyTripBusServices;
import javax.swing.*;
import javax.swing.border.MatteBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.PlainDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
//import java.sql.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.util.Date;
//import java.text.SimpleDateFormat;
//class NumberTextField extends PlainDocument {
//    public NumberTextField() {
//        super();
//    }
//    public void insertString(int offset, String str, AttributeSet attr)
//            throws javax.swing.text.BadLocationException {
//        if (str == null) {
//            return;
//        }
//
//        char[] s = str.toCharArray();
//        int length = 0;
//        // 过滤非数字
//        for (int i = 0; i < s.length; i++) {
//            if ((s[i] >= '0') && (s[i] <= '9')) {
//                s[length++] = s[i];
//            }
//        // 插入内容
//        super.insertString(offset, new String(s, 0, length), attr);
//        }
//    }
//}

public class BookTickets extends JFrame implements ActionListener {

	
	    static JFrame jFrame = new JFrame();
	    public static String currentname;
		JTabbedPane menu;
		JTextField tName , tAge , tSeats , tGender , tEmailID , tDOB, tContactNumber , tAddress , number , tTicketNumber , tOldContact , tNewContact , tConfirmNewContact , tDOB1;
		JLabel Name , Age;
		JLabel ClassChoose;
		JLabel Seats;
		JLabel Gender;
		JLabel EmailID;
		JLabel PickupPoint;
		JLabel DOB;
		JLabel DestinationPoint;
		JLabel ContactNumber;
		JLabel Address;
		JLabel ticket;
		JLabel TicketNumber;
		JLabel OldContact;
		JLabel NewContact;
		JLabel ConfirmNewContact;
		JLabel DOB1;
		JComboBox tClass , tPickupPoint , tDestinationPoint ;
		String ClassChoose1[] = {"1st Class","2st Class","3st Class","4st Class","5st Class","6st Class","7st Class","8st Class","9st Class","10st Class","11st Class","12st Class","13st Class","14st Class","15st Class","16st Class","17st Class","18st Class","19st Class","20st Class","21st Class","22st Class","23st Class"};
		String PickupPoint1[] = {" ","HaiKou","WanNing","BeiJing","ShenZhen","GuangZhou","NamJing","ChangChun","ShangHai","ChengDu","ChangSha","WuHan","HaErBin","ShenYang","HongKong","TaiBei","HeFei","ShiJiaZhuang","ChongQing","ZhengZhou","TaiYuan","JiNan"};
		String DestinationPoint1[] = {" ","HaiKou","WanNing","BeiJing","ShenZhen","GuangZhou","NamJing","ChangChun","ShangHai","ChengDu","ChangSha","WuHan","HaErBin","ShenYang","HongKong","TaiBei","HeFei","ShiJiaZhuang","ChongQing","ZhengZhou","TaiYuan","JiNan"};
		JTabbedPane pane;
		JButton BookNow , print , cancelNow , Change;
		ResultSet countset;
		Integer user_id;
		int ticketno;
		JTextArea Showticket;
	private JTabbedPane tab=new JTabbedPane();	
	public BookTickets(Integer user_id)
	{
		this.user_id = user_id;
		Container BookTickets=new Container(); 

		Container CancelTickets=new Container(); 

		Container UpdateInformation=new Container(); 

		Container PrintTicket=new Container(); 
		
		jFrame.setLayout(null);
		
		//creating a JTabbed Pane
		tab.add(BookTickets,"Book Tickets");
		tab.add(CancelTickets,"Cancel Tickets");
		tab.add(UpdateInformation,"Update Information");
		tab.add(PrintTicket,"Print Ticket");
		getContentPane().add(tab);
		this.setSize(900,600);
		this.setResizable(false);
		this.setTitle("My Trip Bus Services");
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setVisible(true);
		this.setBackground(Color.white);
		this.setLocation(500,100);
		
				//create label Name
				Name = new JLabel("Full Name:");
				Name.setFont(new Font("Calibri" , Font.BOLD,20));
				Name.setBounds(10,50,200,60);
				BookTickets.add(Name);

				//create textfield tName
				tName = new JTextField();
				tName.setFont(new Font("Calibri" , Font.PLAIN,20));
				tName.setBounds(200,60,200,30);
				tName.setBackground(Color.yellow);
				BookTickets.add(tName);

				//create lable Age
				Age = new JLabel("Age:");
				Age.setFont(new Font("Calibri" , Font.BOLD,20));
				Age.setBounds(10,100,200,60);
				BookTickets.add(Age);

				//create textfield tAge
				tAge = new JTextField();
				tAge.setFont(new Font("Calibri" , Font.PLAIN,20));
				tAge.setBounds(200,110,200,30);
				tAge.setBackground(Color.yellow);
				BookTickets.add(tAge);

				//create lable Gender
				Gender = new JLabel("Gender:");
				Gender.setFont(new Font("Calibri",Font.BOLD,20));
				Gender.setBounds(10,150,200,60);
				BookTickets.add(Gender);

				//create textfield tGender
				tGender = new JTextField();
				tGender.setFont(new Font("Calibri",Font.PLAIN,20));
				tGender.setBounds(200,160,200,30);
				tGender.setBackground(Color.yellow);
				BookTickets.add(tGender);

				//create lable EmailID
				EmailID = new JLabel("Email ID:");
				EmailID.setFont(new Font("Calibri",Font.BOLD,20));
				EmailID.setBounds(10,200,200,60);
				BookTickets.add(EmailID);

				//create textfield tEmailID
				tEmailID = new JTextField();
				tEmailID.setFont(new Font("Calibri",Font.PLAIN,20));
				tEmailID.setBounds(200,210,200,30);
				tEmailID.setBackground(Color.yellow);
				BookTickets.add(tEmailID);

				//create lable DOB
				DOB = new JLabel("DOB:");
				DOB.setFont(new Font("Calibri",Font.BOLD,20));
				DOB.setBounds(10,250,200,60);
				BookTickets.add(DOB);

				//create textfield tDOB
				tDOB = new JTextField();
				tDOB.setFont(new Font("Calibri",Font.PLAIN,20));
				tDOB.setBounds(200,260,200,30);
				tDOB.setBackground(Color.yellow);
				BookTickets.add(tDOB);

				//create lable ContactNumber
				ContactNumber = new JLabel("Contact Number:");
				ContactNumber.setFont(new Font("Calibri",Font.BOLD,20));
				ContactNumber.setBounds(10,300,200,60);
				BookTickets.add(ContactNumber);

				//create textfield tContactNumber
				tContactNumber = new JTextField();
				tContactNumber.setFont(new Font("Calibri",Font.PLAIN,20));
				tContactNumber.setBounds(200,310,200,30);
				tContactNumber.setBackground(Color.yellow);
				tContactNumber.setDocument(new NumberTextField());
				BookTickets.add(tContactNumber);

				//create lable Address
				Address = new JLabel("Address:");
				Address.setFont(new Font("Calibri",Font.BOLD,20));
				Address.setBounds(10,350,200,60);
				BookTickets.add(Address);

				//create textfield tAddress
				tAddress = new JTextField();
				tAddress.setFont(new Font("Calibri",Font.PLAIN,20));
				tAddress.setBounds(200,360,200,30);
				tAddress.setBackground(Color.yellow);
				BookTickets.add(tAddress);
	
				//create lable Class
				ClassChoose = new JLabel("Class:");
				ClassChoose.setFont(new Font("Calibri",Font.BOLD,20));
				ClassChoose.setBounds(400,50,200,60);
				BookTickets.add(ClassChoose);
	
				//create combobox tClass
				tClass = new JComboBox(ClassChoose1);
				tClass.setFont(new Font("Calibri",Font.PLAIN,15));
				tClass.setBounds(590,55,200,30);
				tClass.setBackground(Color.yellow);
				BookTickets.add(tClass);
	
				//create lable Seats
				Seats = new JLabel("Seats:");
				Seats.setFont(new Font("Calibri",Font.BOLD,20));
				Seats.setBounds(400,100,200,60);
				BookTickets.add(Seats);
	
				//create textfield tSeats
				tSeats = new JTextField();
				tSeats.setFont(new Font("Calibri",Font.PLAIN,20));
				tSeats.setBounds(590,110,200,30);
				tSeats.setBackground(Color.yellow);
				BookTickets.add(tSeats);
	
				//create lable PickupPoint
				PickupPoint = new JLabel("Pickup Point:");
				PickupPoint.setFont(new Font("Calibri",Font.BOLD,20));
				PickupPoint.setBounds(400,200,200,60);
				BookTickets.add(PickupPoint);
	
				//create combobox tPickupPoint
				tPickupPoint = new JComboBox(PickupPoint1);
				tPickupPoint.setFont(new Font("Calibri",Font.PLAIN,15));
				tPickupPoint.setBounds(590,210,200,30);
				tPickupPoint.setBackground(Color.yellow);
				BookTickets.add(tPickupPoint);
	
				//create lable DestinationPoint
				DestinationPoint = new JLabel("Destination Point:");
				DestinationPoint.setFont(new Font("Calibri",Font.BOLD,20));
				DestinationPoint.setBounds(400,250,200,60);
				BookTickets.add(DestinationPoint);
	
				//create combobox tDestinationPoint
				tDestinationPoint = new JComboBox(DestinationPoint1);
				tDestinationPoint.setFont(new Font("Calibri",Font.PLAIN,15));
				tDestinationPoint.setBounds(590,260,200,30);
				tDestinationPoint.setBackground(Color.yellow);
				BookTickets.add(tDestinationPoint);
	
				//create JButton Book Now
				JButton BookNow=new JButton("Book Now");
				BookNow.setFont(new Font("Calibri",Font.BOLD,20));
				BookNow.addActionListener(this);
				BookNow.setBounds(350,450,150,50);
				BookTickets.add(BookNow);
				jFrame.repaint();
				
				//create lable TicketNumber
				TicketNumber = new JLabel("Ticket Number:");
				TicketNumber.setFont(new Font("Calibri",Font.BOLD,20));
				TicketNumber.setBounds(20,100,200,60);
				CancelTickets.add(TicketNumber);
	
				//create combobox tTicketNumber
				tTicketNumber = new JTextField();
				tTicketNumber.setFont(new Font("Calibri",Font.PLAIN,15));
				tTicketNumber.setBounds(170,110,200,30);
				tTicketNumber.setBackground(Color.yellow);
				CancelTickets.add(tTicketNumber);
				
				//create lable DOB
				DOB1 = new JLabel("DOB:");
				DOB1.setFont(new Font("Calibri",Font.BOLD,20));
				DOB1.setBounds(450,100,200,60);
				CancelTickets.add(DOB1);
	
				//create combobox tDOB
				tDOB1 = new JTextField();
				tDOB1.setFont(new Font("Calibri",Font.PLAIN,15));
				tDOB1.setBounds(520,110,200,30);
				tDOB1.setBackground(Color.yellow);
				CancelTickets.add(tDOB1);
				
				//create JButton Cancel Now
			    cancelNow=new JButton("Cancel Now");
				cancelNow.setFont(new Font("Calibri",Font.BOLD,20));
				cancelNow.addActionListener(this);
				cancelNow.setBounds(350,450,150,50);
				CancelTickets.add(cancelNow);
				jFrame.repaint();
				
				//create lable OldContact
				OldContact = new JLabel("Old Contact:");
				OldContact.setFont(new Font("Calibri",Font.BOLD,20));
				OldContact.setBounds(20,100,200,60);
				UpdateInformation.add(OldContact);
	
				//create combobox tOldContact
				tOldContact = new JTextField();
				tOldContact.setFont(new Font("Calibri",Font.PLAIN,15));
				tOldContact.setBounds(220,110,200,30);
				tOldContact.setBackground(Color.yellow);
				tOldContact.setDocument(new NumberTextField());
				UpdateInformation.add(tOldContact);
				
				//create lable NewContact
				NewContact = new JLabel("New Contact:");
				NewContact.setFont(new Font("Calibri",Font.BOLD,20));
				NewContact.setBounds(20,150,200,60);
				UpdateInformation.add(NewContact);
	
				//create combobox tNewContact
				tNewContact = new JTextField();
				tNewContact.setFont(new Font("Calibri",Font.PLAIN,15));
				tNewContact.setBounds(220,160,200,30);
				tNewContact.setBackground(Color.yellow);
				tNewContact.setDocument(new NumberTextField());
				UpdateInformation.add(tNewContact);
				
				//create lable ConfirmNewContact
				ConfirmNewContact = new JLabel("Confirm New Contact:");
				ConfirmNewContact.setFont(new Font("Calibri",Font.BOLD,20));
				ConfirmNewContact.setBounds(20,200,200,60);
				UpdateInformation.add(ConfirmNewContact);
	
				//create combobox tConfirmNewContact
				tConfirmNewContact = new JTextField();
				tConfirmNewContact.setFont(new Font("Calibri",Font.PLAIN,15));
				tConfirmNewContact.setBounds(220,210,200,30);
				tConfirmNewContact.setBackground(Color.yellow);
				tConfirmNewContact.setDocument(new NumberTextField());
				UpdateInformation.add(tConfirmNewContact);
				
				//create JButton Book Now
				JButton Change=new JButton("Change");
				Change.setFont(new Font("Calibri",Font.BOLD,20));
				Change.addActionListener(this);
				Change.setBounds(350,450,150,50);
				UpdateInformation.add(Change);
				jFrame.repaint();
				
				
				ticket=new JLabel("Enter Ticket Number:");
				ticket.setFont(new Font("Calibri",Font.BOLD,20));
				ticket.setBounds(20,100,200,60);
				PrintTicket.add(ticket);
						
				number=new JTextField();
				number.setFont(new Font("Calibri",Font.PLAIN,15));
				number.setBounds(220,110,200,30);
				number.setBackground(Color.yellow);
				PrintTicket.add(number);
			
				print=new JButton("Print");
				print.setFont(new Font("Calibri",Font.BOLD,20));
				print.setBounds(350,450,150,50);
				PrintTicket.add(print);
				
				Showticket=new JTextArea("Ticket Informations are below");
				Showticket.setBounds(500, 100, 300, 250);
				PrintTicket.add(Showticket);
				
				
				print.addActionListener(this);
				cancelNow.addActionListener(this);
				
				
	}
	
	public static void main(String[] args) {
		//new BookTickets();
		BookTickets.jFrame.repaint();

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println(e.getActionCommand());
		if(e.getActionCommand()=="Book Now") {
			if(tName.getText().equals("")) {
				JOptionPane.showMessageDialog(this, "Name can't be blank !!");
			}
			else {
				insertData();
			}
		}
		 if("Change".equals(e.getActionCommand())) {
			 Change();
			}
		 if("Cancel Now".equals(e.getActionCommand())) {
				Cancel();
			}
		 if("Print".equals(e.getActionCommand())) {
			 if(number.getText().equals("")) {
					JOptionPane.showMessageDialog(this, "Ticket number can't blank !!");
				}
			 else {
					Print();
				}
				
			}
	}
	 
	//Connection method
	
	public static Connection createConnection() {
		Connection con=null;
     try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			con=DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/project?serverTimezone=UTC","root","131026");
		}
		catch(Exception e) {
			System.out.print(e);
		}
		return con;
		
	}
	
	
public void insertData() {
	try {
		Connection con=createConnection();
//		String sql="insert into users values('tName.getText()','tAge.getText()','tGender.getText()','tEmailID.getText()','tContactNumber.getText()','tAddress.getText()','member') where user_name='tName.getText()' ;";
//		String sql="insert into users values(tName.getText(),tAge.getText(),tGender.getText(),tEmailID.getText(),tContactNumber.getText(),tAddress.getText(),member) where user_name='"+tName.getText()+"' ";
//      String sql2="insert into ticket(?,?,?,?,?,?,?) values('"+tDOB1.getText()+"','"+tSeats.getText()+"','"+tPickupPoint.getSelectedItem()+"','"+tDestinationPoint.getSelectedItem()+"','"+tClass.getSelectedItem()+"') where user_name='currentname'";
   		//String get="select * from count;";
   		Statement stmt = con.createStatement();
   	   // countset=stmt.executeQuery(get);
		 long currentTime=System.currentTimeMillis();
		ticketno= (int)currentTime;
//		System.out.println(tDOB.getText());
		String sql2="insert into ticket(user_name,ticketno,date_of_journey,seats,pickup_point,destination_point,class,users_id) values ('"+tName.getText()+"','"+ticketno+"','"+tDOB.getText()+"','"+tSeats.getText()+"','"+tPickupPoint.getSelectedItem()+"','"+tDestinationPoint.getSelectedItem()+"','"+tClass.getSelectedItem()+"','"+this.user_id+"');";
//		String sql2="insert into ticket(ticketno,date_of_journey,seats,pickup_point,destination_point,class,users_id) values ('"+ticketno+"','"+tDOB.getText()+"','"+tSeats.getText()+"','"+tPickupPoint.getSelectedItem()+"','"+tDestinationPoint.getSelectedItem()+"','"+tClass.getSelectedItem()+"','"+this.user_id+"');";
//		String sql4="insert into ticket(ticketno) values('"+ticketno+"')";
//		int b=stmt.executeUpdate(sql4);
//		tTicketNumber = new JTextField(ticketno);
//		int i=stmt.executeUpdate(sql);
		int a=stmt.executeUpdate(sql2);
//		if(i!=0&&a!=0) {
		if(a!=0) {
			JOptionPane.showMessageDialog(this, "Book Successfully");
			JOptionPane.showMessageDialog(this, "The Ticket Number is "+ticketno);

		    }
		else {
			JOptionPane.showMessageDialog(this, "Book Failed");
		}
	
	}
	catch(Exception e) {	
		JOptionPane.showMessageDialog(this, e);
	}
 }
public void Change() {
	String Oldcontact=tOldContact.getText();
	try {
		Connection con=createConnection();
		String query="update users set contact='"+tNewContact.getText()+"' where contact='"+tOldContact.getText()+"'";
//		PreparedStatement pstmt = con.prepareStatement(query);	
//		pstmt.setString(1,Oldcontact);
		Statement pstmt = con.createStatement();
		
		int i=pstmt.executeUpdate(query);
		if(i!=0) {
			JOptionPane.showMessageDialog(this, "Contact Changes");
		    }
		else {
			JOptionPane.showMessageDialog(this, "Old Contact is worng and Contact don't Change");
			}
	}
	catch(Exception e) {	
		System.out.println(e);
	}
}
public void Cancel()
{  
//	String TicketNumber=tTicketNumber.getText();
//	String DOB1=tDOB1.getText();
	try{
		Connection con=createConnection();
		String sql="delete from ticket where ticketno='"+tTicketNumber.getText()+"' ";
//		Statement pstmt = con.createStatement();
		PreparedStatement pstmt = con.prepareStatement(sql);
//		pstmt.setString(1, TicketNumber);
//		pstmt.setString(2, DOB);
    	int i=pstmt.executeUpdate(sql);
    	if(i!=0){
    		JOptionPane.showMessageDialog(this,"Cancel contact successfully");  
    		tTicketNumber.setText("");tDOB1.setText("");
    	}
    	else {
    		JOptionPane.showMessageDialog(this,"Contact not exists"); 
    	}
	}
	catch (Exception e){
		System.out.println(e);
	}   
 }
public void Print()
{
	try{
		Connection con=createConnection();
		String sql="select *from ticket where ticketno='"+number.getText()+"';";
//		Statement stmt = con.createStatement();
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet i=pstmt.executeQuery(sql);
    	if(i.next()){
    		Showticket.setText("TicketNumber is:"+i.getString(1)+"\n"+"Name:"+i.getString(3)+"\n"+"DateofJurney:"+i.getString(4)+"\n"+"class:"+i.getString(5)+"\n"+"pick up point:"+i.getString(6)+"\n"+"destination point:"+i.getString(7)+"\n"+"Seat:"+i.getString(8));
    		JOptionPane.showMessageDialog(this, "Print sucessfully !!");
    		
    	}
    	else {
    		JOptionPane.showMessageDialog(this, "Print unsucessfully,ticket number don't exist !!");
    	}
	}
	catch (Exception e){
		System.out.println(e);
	}   
}
}